package com.intellij.ide.license.impl;

import com.intellij.licensecommon.data.LicenseData;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.util.StringTokenizer;

public abstract class LicenseFile {
    private static final Logger LOG = Logger.getInstance("#com.intellij.ide.license.impl.LicenseFile");
    private static final String JET_PROFILE = "JetProfile";
    private static final String URL_PREFIX = "URL:";
    protected final String c;
    private final String d;

    protected LicenseFile(String paramString1, String paramString2) {
        this.c = paramString1;
        this.d = paramString2;
    }

    public void write(LicenseData parame) throws LicenseSaveException {
        if (LOG.isDebugEnabled()) {
            LOG.debug("writing license data...");
        }
        try {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(this.d);
                fos.write(255);
                fos.write(255);
                if ((parame instanceof ServerLicense)) {
                    write(fos, URL_PREFIX + ((ServerLicense) parame).v());
                } else if ((parame instanceof JetProfileLicenseData)) {
                    write(fos, JET_PROFILE);
                } else {
                    writeTo(parame, fos);
                }
                fos.flush();
            } finally {
                if (fos != null) {
                    fos.close();
                }
            }
        } catch (Exception localException) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("error writing license data:" + localException.getMessage());
            }
            throw new LicenseSaveException(localException);
        }
    }

    protected abstract void writeTo(LicenseData licenseData, OutputStream outputStream) throws IOException;

    protected static void write(OutputStream os, String content)
            throws IOException {
        for (int i = 0; i < content.length(); i++) {
            int j = content.charAt(i);
            int k = (byte) (j & 0xFF);
            int m = (byte) (j >> 8 & 0xFF);
            os.write(k);
            os.write(m);
        }
    }

    public LicenseData read() {
        if (LOG.isDebugEnabled()) {
            LOG.debug("\"reading license data...\"");
        }
        try {
            LicenseData locale = b(e());
            if (LOG.isDebugEnabled()) {
                LOG.debug("read license data:" + (locale != null ? "\"" + locale.getUsername() + "\",\"" + locale.getKey() + "\"" : "null"));
            }
            return locale;
        } catch (IOException localIOException) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("unable to read license data...");
            }
        }
        return null;
    }


    public ServerLicense b() {
        if (LOG.isDebugEnabled()) {
            LOG.debug("\"reading license data...\"");
        }
        try {
            ServerLicense localab = c(e());
            if (LOG.isDebugEnabled()) {
                LOG.debug("read license data:" + (localab != null ? "\"" + localab.v() + "\",\"" + localab.isAccepted() + "\"" : "null"));
            }
            return localab;
        } catch (IOException localIOException) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("unable to read license data...");
            }
        }
        return null;
    }

    public boolean isJetProfile() {
        try {
            return JET_PROFILE.equals(e());
        } catch (IOException localIOException) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("unable to read license data...");
            }
        }
        return false;
    }


    protected static Pair<String, String> a(String paramString) {
        StringTokenizer localStringTokenizer = new StringTokenizer(paramString, "\r\n");
        if (!localStringTokenizer.hasMoreTokens()) {
            return null;
        }
        String str1 = localStringTokenizer.nextToken();
        if (!localStringTokenizer.hasMoreTokens()) {
            return null;
        }
        String str2 = localStringTokenizer.nextToken();
        if ((str2 == null) || (str1 == null)) {
            return null;
        }
        return new Pair(str2, str1);
    }

    protected static boolean isServerUrlStoredInOldFormat(@NotNull String paramString1, @NotNull String paramString2) {
        if (paramString1 == null) {
            throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[]{"0", "com/intellij/ide/license/impl/LicenseFile", "isServerUrlStoredInOldFormat"}));
        }
        if (paramString2 == null) {
            throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[]{"1", "com/intellij/ide/license/impl/LicenseFile", "isServerUrlStoredInOldFormat"}));
        }
        return (paramString2.contains("License")) && (paramString2.contains("Server")) && (paramString1.contains("http"));
    }


    protected abstract LicenseData b(String paramString);


    protected ServerLicense c(String paramString) {
        StringTokenizer localStringTokenizer = new StringTokenizer(paramString, "\r\n");
        if (!localStringTokenizer.hasMoreTokens()) {
            return null;
        }
        String str = localStringTokenizer.nextToken();
        if ((str != null) && (str.startsWith("URL:"))) {
            return ServerLicense.a(str.substring("URL:".length()), getLicenseType());
        }
        return d(paramString);
    }

    public abstract int getLicenseType();


    private ServerLicense d(String paramString) {
        Pair localPair = a(paramString);
        if (localPair == null) {
            return null;
        }
        String str1 = (String) localPair.getFirst();
        String str2 = (String) localPair.getSecond();
        if (!isServerUrlStoredInOldFormat(str2, str1)) {
            return null;
        }
        return ServerLicense.a(str2, getLicenseType());
    }

    private String e() throws IOException {
        if (f()) {
            return g();
        }
        return h();
    }

    private boolean f() throws IOException {
        InputStream is = null;
        try {
            is = i();
            int i = is.read();
            int j = is.read();
            return (i == 255) && (j == 255);
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

    private String g() throws IOException {
        InputStream localInputStream = null;
        try {
            localInputStream = i();
            int i = localInputStream.read();
            int j = localInputStream.read();
            StringBuffer localStringBuffer = new StringBuffer();
            for (; ; ) {
                i = localInputStream.read();
                if (i == -1) {
                    break;
                }
                j = localInputStream.read();
                if (j == -1) {
                    break;
                }
                localStringBuffer.append((char) ((j << 8) + i));
            }
            return localStringBuffer.toString();
        } finally {
            if (localInputStream != null) {
                localInputStream.close();
            }
        }
    }

    private String h()
            throws IOException {
        return FileUtil.loadFile(new File(this.c));
    }

    private InputStream i() throws FileNotFoundException {
        return new BufferedInputStream(new FileInputStream(this.c));
    }
}

